function b = subsref(s,index)
% SUBSREF Define field name indexing for  objects
switch index.type
    case '()'
        error('Unsupported');
    case '.'
        switch index.subs
			case 'h'
				b = s.h;
			case 'x'
				b = s.x;
			case 'site_width'
				b = s.site_width;
			case 'site_width_unit'
				b = s.site_width_unit;
			case 'layer_thickness'
				b = s.layer_thickness;
            case 'dx'
                b = s.dx;
			case 'layer_thickness_unit'
				b = s.layer_thickness_unit;
			case 'r2'
				b = var(s.h,1);
			case 'm2'
                b = calc_m2(s);
            case 'h_mean'
                b = mean(s.h);
			otherwise
				error([propName,' is not a valid property']);
        end
end
end

function m2 = calc_m2(s)
    h = s.h;
    x = s.x;
    dx = s.dx;
    dh = [h(1)-h(end),diff(h)];
    slope = dh./dx;
    m2 = sum(slope.^2)/length(slope);
end
